package com.xy.smartsms.presenter;

import android.content.Context;
import android.util.LruCache;
import android.view.View;
import android.view.ViewGroup;

import com.xy.smartsms.data.SmsItem;
import com.xy.smartsms.util.CommonUtils;
import com.xy.smartsms.vista.BubbleVista;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by admin on 2017/1/13.
 */

public abstract class BubblePresenter extends Presenter<BubbleVista> {
    public static final int TYPE_SIMPLE = 1;
    public static final int TYPE_RICH = 2;
    public static final int TYPE_COMPOUND = 3;
    public static final int TYPE_FEATURE = 4;
    public static final int TYPE_SENSITIVE = 5;

    public final static int BUBBLE_MODE_RICH = 2;
    public final static int BUBBLE_MODE_SIMPLE = 1;
    public final static int BUBBLE_MODE_PRIMITIVE = 0;

    private static final int DUOQU_BUBBLE_DATA_CACHE_SIZE = 200;

    protected Context mContext;
    protected ViewGroup mAdapterView = null;

    protected JSONObject mRichBubbleData = null;
    protected JSONObject mFeatureData = null;
    protected JSONArray mSimpleBubbleData = null;
    protected JSONObject mSensitiveData = null;
    protected SmsItem mMessageItem = null;
    private static final LruCache<String, JSONObject> sBubbleDataCache = new LruCache<String, JSONObject>(
            DUOQU_BUBBLE_DATA_CACHE_SIZE);
    private String mCacheKey = null;
    private Map<String, String> mSmartSmsExtendMap = null;

    protected BubblePresenter(Context context) {
        mContext = context;
    }

    public void setAdapterView(ViewGroup v) {
        mAdapterView = v;
    }

    protected String getCacheKey() {
        return mCacheKey;
    }

    private void setDefaultExtend() {
        if (mMessageItem.getExtend() != null) {
            return;
        }

        if (mSmartSmsExtendMap == null) {
            mSmartSmsExtendMap = new HashMap<>();
            mSmartSmsExtendMap.put("DISABLE_BASE_PARSE", "false");
            mSmartSmsExtendMap.put("ENABLE_EIGENVALUE_PARSE", "true");
            mSmartSmsExtendMap.put("simIndex", "-1");
            mSmartSmsExtendMap.put("ALLOW_PERSONAL_MSG", "true");
            mSmartSmsExtendMap.put("isUseNewAction", "true");
            mSmartSmsExtendMap.put("ref_basevalue", "true");
        }

        mMessageItem.setExtend(mSmartSmsExtendMap);
    }

    public static void clearCache() {
        if (sBubbleDataCache != null) {
            sBubbleDataCache.evictAll();
        }
    }

    protected void putBubbleDataToCache(JSONObject value) {
        String key = getCacheKey();
        if (key == null || value == null) {
            return;
        }

        synchronized (sBubbleDataCache) {
            sBubbleDataCache.put(key, value);
        }
    }

    protected JSONObject getBubbleDataFromCache() {
        String key = getCacheKey();
        if (key == null) {
            return null;
        }

        return sBubbleDataCache.get(key);
    }

    private void reInit() {
        mRichBubbleData = null;
        mFeatureData = null;
        mSimpleBubbleData = null;
        mSensitiveData = null;
    }

    public void setData(SmsItem item, boolean isScrolling) {
        if (mMessageItem != null && mMessageItem.getMsgId() != item.getMsgId()) {
            // presenter has been reused due to the view reuse
            reInit();
        }

        mCacheKey = CommonUtils.getCacheKey(String.valueOf(item.getMsgId()), item.getReceiveTime());
        mMessageItem = item.setCacheKey(mCacheKey)
                .setScrolling(isScrolling);
        setDefaultExtend();
        mRichBubbleData = getBubbleDataFromCache();
    }

    public abstract int getBubbleMode();

    public abstract void setBubbleMode(int mode);

    public abstract void load(int type);
}
